package com.ccrfid.rmc6.lbservice3.manager;

import com.ccrfid.rmc6.entity.alarm.AlarmRecordEntity;
import com.ccrfid.rmc6.entity.alarm.AlarmRuleEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelMutualGroupMemberEntity;
import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.lbservice3.mq.MqLocationAlarm;
import com.ccrfid.rmc6.lbservice3.mq.MqPersonnelAlarm;
import com.ccrfid.rmc6.lbservice3.mq.MqPersonnelGroupAlarm;
import com.ccrfid.rmc6.service.alarm.AlarmNotifyService;
import com.ccrfid.rmc6.service.alarm.AlarmRecordService;
import com.ccrfid.rmc6.service.alarm.AlarmRuleService;
import com.ccrfid.rmc6.service.personnel.MutualGroupService;
import com.ccrfid.rmc6.service.personnel.PersonnelService;
import com.ccrfid.rmc6.service.poi.PoiLocationService;
import com.ccrfid.rmc6.util.JsonUtil;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Component
public class MPAlarmManager {
    private static final Logger logger = LoggerFactory.getLogger(MPAlarmManager.class);

    @Autowired
    private EventBus eventBus;
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private PoiLocationService poiLocationService;
    @Autowired
    private AlarmRuleService alarmRuleService;
    @Autowired
    private AlarmRecordService alarmRecordService;
    @Autowired
    private AlarmNotifyService alarmNotifyService;
    @Autowired
    private MutualGroupService mutualGroupService;

    @PostConstruct
    private void init() {
        eventBus.register(this);
    }

    @Subscribe
    public void onMqPersonnelAlarm(MqPersonnelAlarm personnelAlarm) {
        PoiLocationEntity locationEntity = poiLocationService.getPoiLocationById(personnelAlarm.getLocationId());
        AlarmRuleEntity alarmRuleEntity = alarmRuleService.getAlarmRule(personnelAlarm.getRuleId());
        PersonnelEntity personnelEntity = personnelService.getPersonnelById(personnelAlarm.getPersonnelId());

        if (locationEntity != null && alarmRuleEntity != null && personnelEntity != null) {
            AlarmRecordEntity recordEntity = new AlarmRecordEntity();
            recordEntity.setPersonnelId(personnelEntity.getId());
            recordEntity.setPersonnelName(personnelEntity.getPersonnelName());
            recordEntity.setPersonnelIds(null);
            recordEntity.setNameAcronym(personnelEntity.getNameAcronym());
            recordEntity.setCardNumber(personnelEntity.getCardNumber());
            recordEntity.setLocationId(locationEntity.getId());
            recordEntity.setLocationName(locationEntity.getLocationName());
            recordEntity.setAlarmRuleId(alarmRuleEntity.getId());
            recordEntity.setAlarmRuleName(alarmRuleEntity.getAlarmRuleName());
            recordEntity.setAlarmType(alarmRuleEntity.getAlarmType());
            recordEntity.setAlarmTypeName(alarmRuleService.getAlarmRuleTypeName(alarmRuleEntity.getAlarmType()));
            recordEntity.setAlarmLevel(alarmRuleEntity.getAlarmLevel());
            recordEntity.setAlarmTime(new Timestamp(personnelAlarm.getTime()));
            recordEntity.setAlarmLinkageMode(alarmRuleEntity.getAlarmLinkageMode());
            recordEntity = alarmRecordService.createAlarmRecord(recordEntity);
            //
            alarmNotifyService.postAlarmNotify(personnelEntity.getId(), recordEntity);
            logger.info("notify MqPersonnelAlarm, personnelId: {}, alarmRecord: {}", personnelEntity.getId(), recordEntity);
        } else {
            logger.debug("ignore MqPersonnelAlarm: {}, alarmRuleEntity: {}, locationEntity: {}, personnelEntity: {}",
                    personnelAlarm, alarmRuleEntity, locationEntity, personnelEntity);
        }
    }

    @Subscribe
    public void onMqLocationAlarm(MqLocationAlarm locationAlarm) {
        PoiLocationEntity locationEntity = poiLocationService.getPoiLocationById(locationAlarm.getLocationId());
        AlarmRuleEntity alarmRuleEntity = alarmRuleService.getAlarmRule(locationAlarm.getRuleId());
        if (locationAlarm.getCount() > 0) {
            Long personnelId = locationAlarm.getPersonnels().get(0);
            PersonnelEntity personnelEntity = personnelService.getPersonnelById(personnelId);

            if (locationEntity != null && alarmRuleEntity != null && personnelEntity != null) {
                AlarmRecordEntity recordEntity = new AlarmRecordEntity();
                recordEntity.setPersonnelId(personnelEntity.getId());
                recordEntity.setPersonnelName(personnelEntity.getPersonnelName() + "..等" + locationAlarm.getCount() + "人");
                recordEntity.setPersonnelIds(JsonUtil.INSTANCE.writeValueAsString(locationAlarm.getPersonnels()));
                recordEntity.setNameAcronym(personnelEntity.getNameAcronym());
                recordEntity.setCardNumber(personnelEntity.getCardNumber());
                recordEntity.setLocationId(locationEntity.getId());
                recordEntity.setLocationName(locationEntity.getLocationName());
                recordEntity.setAlarmRuleId(alarmRuleEntity.getId());
                recordEntity.setAlarmRuleName(alarmRuleEntity.getAlarmRuleName());
                recordEntity.setAlarmType(alarmRuleEntity.getAlarmType());
                recordEntity.setAlarmTypeName(alarmRuleService.getAlarmRuleTypeName(alarmRuleEntity.getAlarmType()));
                recordEntity.setAlarmLevel(alarmRuleEntity.getAlarmLevel());
                recordEntity.setAlarmTime(new Timestamp(locationAlarm.getTime()));
                recordEntity.setAlarmLinkageMode(alarmRuleEntity.getAlarmLinkageMode());
                recordEntity = alarmRecordService.createAlarmRecord(recordEntity);
                //
                alarmNotifyService.postAlarmNotify(personnelEntity.getId(), recordEntity);
                logger.info("notify MqLocationAlarm, personnelId: {}, alarmRecord: {}", personnelEntity.getId(), recordEntity);
            } else {
                logger.debug("ignore MqLocationAlarm: {}, alarmRuleEntity: {}, locationEntity: {}, personnelEntity: {}",
                        locationAlarm, alarmRuleEntity, locationEntity, personnelEntity);
            }
        } else {
            logger.debug("ignore MqLocationAlarm: {}", locationAlarm);
        }
    }

    @Subscribe
    public void onMqPersonnelGroupAlarm(MqPersonnelGroupAlarm groupAlarm) {
        AlarmRuleEntity alarmRuleEntity = alarmRuleService.getAlarmRule(groupAlarm.getRuleId());
        List<PersonnelMutualGroupMemberEntity> personnelList =
                mutualGroupService.getPersonnelMutualGroupMemberList(groupAlarm.getGroupId());

        if (alarmRuleEntity != null && personnelList != null && personnelList.size() > 0) {
            AlarmRecordEntity recordEntity = new AlarmRecordEntity();
            List<Long> personnelIds = new ArrayList<>();
            for (PersonnelMutualGroupMemberEntity entity : personnelList) {
                personnelIds.add(entity.getPersonnelId());
            }
            PersonnelEntity personnelEntity = personnelService.getPersonnelById(personnelList.get(0).getPersonnelId());
            if (personnelEntity != null) {
                recordEntity.setPersonnelId(personnelEntity.getId());
                recordEntity.setPersonnelName(personnelEntity.getPersonnelName());
                recordEntity.setPersonnelName(personnelEntity.getPersonnelName() + "..等" + personnelList.size() + "人");
                recordEntity.setPersonnelIds(JsonUtil.INSTANCE.writeValueAsString(personnelIds));
                recordEntity.setNameAcronym(personnelEntity.getNameAcronym());
                recordEntity.setCardNumber(personnelEntity.getCardNumber());
            }
            PoiLocationEntity locationEntity = poiLocationService.getPoiLocationById(alarmRuleEntity.getLocationId());
            if (locationEntity != null) {
                recordEntity.setLocationId(locationEntity.getId());
                recordEntity.setLocationName(locationEntity.getLocationName());
            }
            recordEntity.setAlarmRuleId(alarmRuleEntity.getId());
            recordEntity.setAlarmRuleName(alarmRuleEntity.getAlarmRuleName());
            recordEntity.setAlarmType(alarmRuleEntity.getAlarmType());
            recordEntity.setAlarmTypeName(alarmRuleService.getAlarmRuleTypeName(alarmRuleEntity.getAlarmType()));
            recordEntity.setAlarmLevel(alarmRuleEntity.getAlarmLevel());
            recordEntity.setAlarmTime(new Timestamp(groupAlarm.getTime()));
            recordEntity.setAlarmLinkageMode(alarmRuleEntity.getAlarmLinkageMode());
            recordEntity = alarmRecordService.createAlarmRecord(recordEntity);
            //
            alarmNotifyService.postAlarmNotify(personnelEntity.getId(), recordEntity);
        } else {
            logger.debug("ignore MqPersonnelGroupAlarm: {}, alarmRuleEntity: {}, personnelList: {}",
                    groupAlarm, alarmRuleEntity, personnelList);
        }
    }

    @JmsListener(destination = "mgv_mutual_alarm", containerFactory = "topicListenerCF")
    public void onMgvMutualGroupAlarm(String content) {
        logger.debug("recv from topic[mgv_mutual_alarm]: {}", content);
        Long[] groupIds = JsonUtil.INSTANCE.readValue(content, Long[].class);
        if (groupIds != null && groupIds.length > 0) {
            for (Long groupId : groupIds) {
                List<PersonnelMutualGroupMemberEntity> personnelList =
                        mutualGroupService.getPersonnelMutualGroupMemberList(groupId);

                if (personnelList != null && personnelList.size() > 0) {
                    AlarmRecordEntity recordEntity = new AlarmRecordEntity();
                    List<Long> personnelIds = new ArrayList<>();
                    for (PersonnelMutualGroupMemberEntity entity : personnelList) {
                        personnelIds.add(entity.getPersonnelId());
                    }
                    PersonnelEntity personnelEntity = personnelService.getPersonnelById(personnelList.get(0).getPersonnelId());
                    if (personnelEntity != null) {
                        recordEntity.setPersonnelId(personnelEntity.getId());
                        recordEntity.setPersonnelName(personnelEntity.getPersonnelName());
                        recordEntity.setPersonnelName(personnelEntity.getPersonnelName() + "..等" + personnelList.size() + "人");
                        recordEntity.setPersonnelIds(JsonUtil.INSTANCE.writeValueAsString(personnelIds));
                        recordEntity.setNameAcronym(personnelEntity.getNameAcronym());
                        recordEntity.setCardNumber(personnelEntity.getCardNumber());
                        PoiLocationEntity locationEntity = personnelService.getPersonnel3LevelLocation(personnelEntity.getId());
                        if (locationEntity != null) {
                            recordEntity.setLocationId(locationEntity.getId());
                            recordEntity.setLocationName(locationEntity.getLocationName());
                        }
                    }
                    recordEntity.setAlarmRuleId(0L);
                    recordEntity.setAlarmRuleName("三联号告警");
                    recordEntity.setAlarmType(450);
                    recordEntity.setAlarmTypeName("三联号告警");
                    recordEntity.setAlarmLevel(401);
                    recordEntity.setAlarmTime(new Timestamp(System.currentTimeMillis()));
                    recordEntity.setAlarmLinkageMode("[422]");
                    recordEntity = alarmRecordService.createAlarmRecord(recordEntity);
                    //
                    alarmNotifyService.postAlarmNotify(personnelEntity.getId(), recordEntity);
                } else {
                    logger.debug("ignore MgvMutualGroupAlarm: {}, mutualGroup: {}, personnelList: {}",
                            groupId, personnelList);
                }
            }
        }
    }
}
